package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*用为事务，所以在service中添加intefaceClass*/
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //limit
        PageHelper.startPage(currentPage,pageSize);
        //装载有页面数据，其实可以直接返回Page,但是公司一般自己定义页面数据，也就是PageResult,page中的数据提取出来再封装在pageResult中
        Page<CheckItem> page = checkItemDao.findCheckItemByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        PageResult pageResult = new PageResult(total, result);

        return pageResult;
    }

    //这里去掉了@OverRide
    public void delete(Integer id) throws RuntimeException {
        //检查检查项与检查组是否关联
        Long count = checkItemDao.findCountByCheckItemId(id);
        if(count>0){
           throw  new RuntimeException("检查项被关联，无法被删除");
        }
        checkItemDao.deleteById(id);

    }

    @Override
    public CheckItem findItemById(Integer id) {
        CheckItem item = checkItemDao.findItemById(id);
        return item;
    }

    @Override
    public void updateEdit(CheckItem checkItem) {
        checkItemDao.updateEdit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
